package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.CalcinationDesulfurizationDenitration;
import com.hkts.naturalgas.base.entity.CalcinationPurificationBleaching;
import com.hkts.naturalgas.base.entity.CalcinationPurifyWetElectricity;
import com.hkts.naturalgas.base.mapper.CalcinationPurificationBleachingMapper;
import com.hkts.naturalgas.base.service.ICalcinationPurificationBleachingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class CalcinationPurificationBleachingServiceImpl extends ServiceImpl<CalcinationPurificationBleachingMapper, CalcinationPurificationBleaching> implements ICalcinationPurificationBleachingService {


    @Autowired
    private CalcinationPurificationBleachingMapper calcinationPurificationBleachingMapper;

    @Override
    public IPage<CalcinationPurificationBleaching> selectPage(Page<CalcinationPurificationBleaching> page, CalcinationPurificationBleaching calcinationPurificationBleaching){
            QueryWrapper<CalcinationPurificationBleaching> query=Wrappers.query();
            QueryWrapper<CalcinationPurificationBleaching> entityWrapper=getEntityWrapper(query, calcinationPurificationBleaching);
            return calcinationPurificationBleachingMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<CalcinationPurificationBleaching> selectList(CalcinationPurificationBleaching calcinationPurificationBleaching){
            QueryWrapper<CalcinationPurificationBleaching> query=Wrappers.query();
            QueryWrapper<CalcinationPurificationBleaching> entityWrapper=getEntityWrapper(query, calcinationPurificationBleaching);
            return calcinationPurificationBleachingMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CalcinationPurificationBleaching> getEntityWrapper(QueryWrapper<CalcinationPurificationBleaching> entityWrapper,CalcinationPurificationBleaching calcinationPurificationBleaching){
            //条件拼接
            return entityWrapper;
    }
    @Override
    public IPage<CalcinationPurificationBleaching> selectPageByTime(Page<CalcinationPurificationBleaching> page, String startDate, String endDate){
        QueryWrapper<CalcinationPurificationBleaching> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(CalcinationPurificationBleaching::getTime);
        IPage<CalcinationPurificationBleaching> hktsTailGasPage = calcinationPurificationBleachingMapper.selectPage(page, query);
        return hktsTailGasPage;
    }

    @Override
    public void importByFiles(List<CalcinationPurificationBleaching> list) {
       this.saveBatch(list);
    }

    @Override
    public IPage<CalcinationPurificationBleaching> getBrokenLineGraph(Page<CalcinationPurificationBleaching> page, String startDate, String endDate){
        QueryWrapper<CalcinationPurificationBleaching> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(CalcinationPurificationBleaching::getTime);
        IPage calcinationDesulfurizationDenitrations = calcinationPurificationBleachingMapper.selectPage(page,query);
        List<CalcinationPurificationBleaching> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(CalcinationPurificationBleaching cPB){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("煅烧A风机电流(A)",cPB.getDsjhFanadl());
        data.put("煅烧A风机频率(Hz)",cPB.getDsjhFanasv());
        data.put("煅烧B风机电流(A)",cPB.getDsjhFanbdl());
        data.put("煅烧B风机频率(Hz)",cPB.getDsjhFanbsv());
        data.put("煅烧C风机电流(A)",cPB.getDsjhFancdl());
        data.put("煅烧C风机频率(Hz)",cPB.getDsjhFancsv());
        data.put("煅烧脱白塔进口湿度(%)",cPB.getDsjhSidu());
        data.put("煅烧脱白塔进口温度(℃)",cPB.getDsjhTe103());
        data.put("煅烧脱白塔出口温度(℃)",cPB.getDsjhTe108());
        data.put("煅烧脱白塔出口湿度(%)",cPB.getDsjhTe110());
        data.put("time",cPB.getTime());
        return data;
    }
    /** 功能描述:
    *通过时间查询实体
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-14 10:54
    * @Description //TODO
    */
    @Override
    public CalcinationPurificationBleaching selectByTime(LocalDateTime toLocalDateTime) {
        QueryWrapper<CalcinationPurificationBleaching> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq(CalcinationPurificationBleaching.TIME,toLocalDateTime);
        CalcinationPurificationBleaching one = this.getOne(entityWrapper);
        return one;
    }

    @Override
    public List<CalcinationPurificationBleaching> selectByTimes(LocalDateTime localDateTime, LocalDateTime localDateTime1) {
        QueryWrapper<CalcinationPurificationBleaching> entityWrapper=new QueryWrapper<>();
        entityWrapper.ge(CalcinationPurificationBleaching.TIME,localDateTime);
        entityWrapper.lt(CalcinationPurificationBleaching.TIME,localDateTime1);
        List<CalcinationPurificationBleaching> calcinationPurificationBleachings = calcinationPurificationBleachingMapper.selectList(entityWrapper);
        return calcinationPurificationBleachings;
    }
    @Override
    public void deleteAll(List<String> collect) {
        calcinationPurificationBleachingMapper.deleteAllData();
    }
}
